package com.yixin.ystartlibrary.base;

import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.wang.avi.AVLoadingIndicatorView;
import com.yixin.ystartlibrary.R;
import com.yixin.ystartlibrary.utils.LoadingDialogUtils;
import com.yixin.ystartlibrary.utils.StatusBarUtil;

import java.util.ArrayList;


public abstract class BaseAppCompatActivity extends RxAppCompatActivity {

    public static final String KEY_DATA = "key_data";
    public static final String KEY_TITLE = "key_title";
    public static final String KEY_EXTRA = "key_extra";
    public static final String KEY_TYPE = "key_type";

    TextView mLoadingFailView;
    FrameLayout mLoadingFrame;
    FrameLayout mLoadingFailFrame;
    AVLoadingIndicatorView mLoadingView;
    protected  TextView mToolbarTitle;
    protected Toolbar mToolBar;
    View mHorizontalLine;
    LinearLayout mRootLayout;
    private AlertDialog mLoadingDialog;

    protected void showLoadingDialog(){
        if(mLoadingDialog == null)
            mLoadingDialog = LoadingDialogUtils.create(this, "");
        mLoadingDialog.show();
    }

    protected void dismissLoadingDialog(){
        if(mLoadingDialog != null && mLoadingDialog.isShowing())
            mLoadingDialog.dismiss();
    }

    protected BaseAppCompatActivity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.simple_activity_base_layout);
//        ToastUtils.showLongToast(this, getClass().getSimpleName());
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        mActivity = this;
        baseInit();
        mLoadingDialog = LoadingDialogUtils.create(this, "");

        //白色状态栏、白色标题栏、黑色状态栏图标
        statusBarIconWhiteBlackColor(true);
        StatusBarUtil.StatusBarLightMode(mActivity);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT &&
                Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            StatusBarUtil.setPaddingSmart(mActivity, mToolBar);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLoadingView.hide();

    }

    private void baseInit() {
        mRootLayout = (LinearLayout) findViewById(R.id.root_layout);
        mToolBar = (Toolbar) findViewById(R.id.tool_bar);
        mToolbarTitle = (TextView) findViewById(R.id.toolbar_title);
        mHorizontalLine = findViewById(R.id.horizontal_line_view);
        mLoadingFailView = (TextView) findViewById(R.id.loading_fail_view);
        mLoadingFrame = (FrameLayout) findViewById(R.id.frame_loading);
        mLoadingView = (AVLoadingIndicatorView) findViewById(R.id.loading_view);
        mLoadingFailFrame = (FrameLayout) findViewById(R.id.frame_loading_fail);
        mToolBar.setTitle(" ");
        setSupportActionBar(mToolBar);
        mLoadingFailView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                query();

            }
        });
    }

    public void query() {
        if (mLoadingFailFrame.getVisibility() != View.GONE) {
            mLoadingFailFrame.setVisibility(View.GONE);
        }
        mLoadingFrame.setVisibility(View.VISIBLE);
        mLoadingView.show();
    }

    public void queryComplete() {
        mLoadingFrame.setVisibility(View.GONE);
    }

    public void queryError() {
        queryComplete();
        mLoadingFailFrame.setVisibility(View.VISIBLE);
    }

    public LinearLayout getRootLayout() {
        return mRootLayout;
    }

    public View getToolbarLogoIconView() {
        //check if contentDescription previously was set
        boolean hadContentDescription = android.text.TextUtils.isEmpty(mToolBar.getLogoDescription());
        String contentDescription = String.valueOf(!hadContentDescription ? mToolBar.getLogoDescription() : "logoContentDescription");
        mToolBar.setLogoDescription(contentDescription);
        ArrayList<View> potentialViews = new ArrayList<View>();
        //find the view based on it's content description, set programatically or with android:contentDescription
        mToolBar.findViewsWithText(potentialViews, contentDescription, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
        //Nav icon is always instantiated at this point because calling setLogoDescription ensures its existence
        View logoIcon = null;
        if (potentialViews.size() > 0) {
            logoIcon = potentialViews.get(0);
        }
        //Clear content description if not previously present
        if (hadContentDescription)
            mToolBar.setLogoDescription(null);
        return logoIcon;
    }

    @Override
    public void setContentView(int layoutId) {
        View view = LayoutInflater.from(mActivity).inflate(layoutId, mRootLayout, false);
        setContentView(view);
    }

    @Override
    public void setContentView(View view) {
        mRootLayout.addView(view);
    }

    public void hideToolBar() {
        if (mToolBar.getVisibility() != View.GONE) {
            mToolBar.setVisibility(View.GONE);
        }
    }

    public void showToolBar() {
        if (mToolBar.getVisibility() != View.VISIBLE) {
            mToolBar.setVisibility(View.VISIBLE);
        }
    }

    public void translucentStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
    }

    public void translucentNavigationBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION | localLayoutParams.flags);
        }
    }

    public void setStatusBarColor(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(color);
        }
    }

    /**
     * 状态栏图标颜色(白色、黑色)切换,默认白色
     */
    public void statusBarIconWhiteBlackColor(boolean isDefault) {
        if (isDefault) {
            getToolbarTitleView().setTextColor(ContextCompat.getColor(mActivity, R.color.white));
        } else {
            getToolbarTitleView().setTextColor(ContextCompat.getColor(mActivity, R.color.black));
        }
    }

    public Toolbar getToolBar() {
        return mToolBar;
    }

    public TextView getToolbarTitleView() {
        return mToolbarTitle;
    }

    public void setEnabledHorizontalLine(boolean isEnabled) {
        int v = isEnabled ? View.VISIBLE : View.GONE;
        mHorizontalLine.setVisibility(v);
    }

    /**
     * 显示在标题栏中间的文字.
     */
    public void setCenterTitle(CharSequence title) {
        mToolbarTitle.setText(title);
    }

    public void setNavigationIcon(int resId) {
        mToolBar.setNavigationIcon(resId);
    }

    public void setNavigationIcon(Drawable icon) {
        mToolBar.setNavigationIcon(icon);
    }

    public void setEnabledNavigation(boolean isEnable) {
        ActionBar bar = getSupportActionBar();
        if (bar == null) return;
        bar.setDisplayHomeAsUpEnabled(isEnable);
        if (isEnable) {
//            bar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP);
            mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    protected  abstract  void init();

}
